#ifndef CAMERA_UTIL_H
#define CAMERA_UTIL_H

class CommonPort
{
public:
    CommonPort();

    static int      getMMCDeviceName(char *name);
    // 获取磁盘可用空间
    static long     getAvailDiskSpace(const char *path);
    // 获取磁盘总空间
    static long     getFullDiskSpace(const char *path);
    // 获取磁盘可用空间
    static long     getDiskSpace(const char *path);
    static int      getDiskFreeSpace(const char *lpRootPathName, int *lpSectorsPerCluster, int *lpBytesPerSector, int *lpNumberOfFreeClusters, int *lpTotalNumberOfClusters);
    // 返回，计算的目录下的文件数量统计
    static long     getDirSpace(const char *dir);
    // printf uptime
    static void     printSystemUptime();

    static uint64_t getCurrentTime_ms();
    static uint64_t getCurrentTime_us(void);
    static uint64_t getCurrentCPUTime_us();
    static uint64_t getCurrentCPUTime_ms();
    static uint32_t getCurrentCPUTime_s();

    static bool s_system(const char *cmd);
    static bool s_chdir(const char *cmd);
    static bool s_system_text(const char* ptr, char* buf, const int32_t buf_len, int32_t* len);
    static unsigned int getCrc32(const void *buf, int size);

    static int      genSessionToken();

    static unsigned int *DWLMapRegisters(int mem_dev, unsigned int base, unsigned int regSize, unsigned int write);
    static void          DWLUnmapRegisters(const void *io, unsigned int regSize);


    static int      recvn(int fd, void *buffer, int n);
    static int      sendn(int fd, const void *buffer, int n);
    static bool     checkSockDead(int fd);
    static int      write_all(int fd, const void *buf, size_t count);
    static int      fwrite_all(const void *ptr, size_t size, size_t nmemb, FILE *stream);
    static ssize_t  read_all(int fd, char *buf, size_t count);

    static int openComPort(int com_port);
    static int setComOpt(int fd, int nSpeed, int nBits, char nEvent, int nStop);
    static int openLocalSocket(int port = 0);
    static int sendLocalMessage( int inSock, const void *inMsg, int inLen);
    static int openNetlinkSocket();

    //! 监听ipv6端口
    //! 创建socket用于服务端的监听
    static int allocSocket(const char *ip, int port, bool isTcp = true, bool isIpv4 = true);
    //! 创建socket用于客户端的连接
    static int allocClientSocket(const char *ip, int port, bool isTcp = true, bool isIpv4 = true);

    /**
     * @brief allocClientSocket
     * @param ipAddr [out]
     * typedef union
     * {
     *     struct sockaddr         sa;
     *     struct sockaddr_in      v4;
     *     struct sockaddr_in6     v6;
     * } sockaddr_ip;
     * @param ip
     * @param port
     * @param isTcp
     * @param isIpv4
     * @return sock fd
     * @details 创建socket用于客户端的连接
     */
    static int allocClientSocket(void *ipAddr, const char *ip, int port, bool isTcp = true, bool isIpv4 = true);

    static int allocLocalSocket(const char *sockfile, bool isHost = false);
    //! log 收集，用于收集标准数据的数据
    static int allocSyslogLocalSocket(bool isHost = false);

    static int isExistIp(int sock_fd);
    static void str2Mac(char mac[13], unsigned char dig[6]);
    static void mac2Str(unsigned char dig[6], char mac[20]);
    static char *getIpAddress(const char *iface);

    static int allocUDPServiceSocket(const char *ip, int port);
    static int allocUDPClientSocket(const char *ip, int port);

    static bool havaNetInterface(const char *name);
private:
    static inline unsigned long kscale(unsigned long b, unsigned long bs)
    {
        return (b * (unsigned long long)bs + 1024 / 2) / 1024;
    }

};

#endif // CAMERA_UTIL_H
